<?php

require_once 'base.php';

function getGroupIDByName($name) {
    $i = 0;
    $array = bab_getGroups();
    while ($array['name'][$i] != $name) {
        $i++;
    }
    return $array['id'][$i];
}

function unsDepartement_getAdminSectionMenus(&$url, &$text) {
    static $j = 0;

    if ($j == 0) {
        $text = 'UNS Departement';
        $url = $GLOBALS['babAddonUrl'] . "admin";
        $j++;
        return true;
    }

    return false;
}

function unsDepartement_getUserSectionMenus(&$url, &$text) {
    static $j = 0;
    if ($j == 0) {
        $text = 'UNS Departement';
        $url = $GLOBALS['babAddonUrl'] . "user";
        $j++;
        return true;
    }
    return false;
}

function unsDepartement_onSectionCreate(&$title, &$content) {
    return false;
}

function unsDepartement_upgrade($version_base, $version_ini) {
    
    global $babDB;

    include_once $GLOBALS['babInstallPath'] . "utilit/devtools.php";

    /* CREATION DES TABLES */
    $addon = bab_getAddonInfosInstance('UNSDepartement');
    $tables = new bab_synchronizeSql($addon->getPhpPath().'dump.sql');
    
    if ($tables->isCreatedTable('unsDepartement_levels'))
    {
        $babDB->db_query("INSERT INTO `unsDepartement_levels` VALUES (null, 'DUT 1', 1),(null, 'DUT 2', 2),(null, 'Licence Pro', 3);");
    }
    
    $babDB->db_query("DROP VIEW IF EXISTS `unsDepartement_view_formationsList`;");
        $babDB->db_query("CREATE VIEW `unsDepartement_view_formationsList`
                          AS
                          SELECT `unsDepartement_formations`.`id` as `formation_id`, `departement_name`, `unsDepartement_levels`.`id` as `level_id`, `level_year`, `level_name`, `formation_name`, `formation_coordinatorID`
                          FROM `unsDepartement_formations`, `unsDepartement_levels`, `unsDepartement_departements`
                          WHERE `formation_departementID` = `unsDepartement_departements`.`id`
                          AND `formation_levelID` = `unsDepartement_levels`.`id`
                          ORDER BY `departement_name`, `level_year`, `formation_name`;
                          ");


    /* CREATION DES GROUPES PAR DEFAULT */
    $nameGroupModule = "uns_Groups";
    bab_createGroup($nameGroupModule, "Groupe contenant tous les sous groupes du modules departement", 0);
    bab_createGroup("uns_Personnel", "Groupe contenant tous les membres du personnel", 0, getGroupIDByName($nameGroupModule));

    return true;
}

function unsDepartement_onDeleteAddon() {
    return true;
}

?>
